<?PHP
require_once("PropertyObject.php");
require_once("SQL.php");

class User extends PropertyObject
{
	private $id;
	private $login;
	private $hashed_password;
	private $reputation;
	private $last_add_time;
	private $loaded;
	
	function __construct() 	
	{
		$this->id=-1;
		$this->login="";
		$this->hashed_password="";
		$this->reputation=0;
		$this->last_add_time=0;
		$this->loaded=false;
	}
	
	function __destruct()
	{
		//
	}
	
	function load(&$SQL_Handle)
	{
		assert($this->loaded === false);
		
		if(isset_id())
		{
			$result = $SQL_Handle->query("Select * from users where id = ".$this->id." LIMIT 1;");
			if(!$result)
			{
				$this->loaded = false;
				throw new Exception('Unable to load user. No user in db with id '.$id.'.');
				return false;
			}
			
			$result_array = mysqli_fetch_assoc($result);
			
			if(!$result_array)
			{
				$this->loaded = false;
				throw new Exception('Unable to load user. Internal error while loading user using '.$id.'.');
				return false;
			}
			
			$this->$login = $result_array['nick'];
			$this->hashed_password= $result_array['password'];
			//$this->reputation=0;
			//$this->last_add_time=;
			//trzeba dodac atrybuty do modelu.
			
			$result->free();
			$this->loaded = true;
		}
		else if(isset_login())
		{
			//$SQL_Handle->query("Select * from users where login = ".$this->login.";");
			$this->loaded = true;
		}
		else
		{
			$this->loaded = false;
			throw new Exception('Unable to load user due to lack of information about it.');
			return false;
		}
		return true;
	}
	
	function save(&$SQL_Handle)
	{
		assert($this->loaded === true);
		$retval == true;
		try
		{
		//$SQL_Handle->query("update users  set ... where id = ".$this->id.";");
		}
		catch(Exception $e) {$retval = false;}
		
		return $retval;
	}
	
	function isset_id(){return $this->id != -1;}
	function get_id(){return $this->id;}
	function set_id($value){$this->id= (int) $value;}
	function unset_id(){$this->id = -1;}


	function isset_login(){return $this->login != "";}
	function get_login(){return $this->login;}
	function set_login($value){$this->login=$value;}
	function unset_login(){$this->login = "";}


	function isset_hashed_password(){return $this->hashed_password != "";}
	function get_hashed_password(){return $this->hashed_password;}
	function set_hashed_password($value){$this->hashed_password=$value;}
	function unset_hashed_password(){$this->hashed_password = "";}


	function isset_reputation(){return $this->reputation != 0;}
	function get_reputation(){return $this->reputation;}
	function set_reputation($value){$this->reputation=(int) $value;}
	function unset_reputation(){$this->reputation = 0;}


	function isset_last_add_time(){return $this->last_add_time != 0;}
	function get_last_add_time(){return $this->last_add_time;}
	function set_last_add_time($value){$this->last_add_time=(int) $value;}
	function unset_last_add_time(){$this->last_add_time = 0;}


	function isset_loaded(){return $this->loaded != false;}
	function get_loaded(){return $this->loaded;}
	function set_loaded($value){$this->loaded=(bool)$value;}
	function unset_loaded(){$this->loaded = false;}

}

?>